// pages/basic/basic.js
const app = getApp()

Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isLoad: Boolean,
    swiperList: Array,
    favorFilmList: Array,
    newFilmList: Array,
    currentCity: String,
    prevueFilmList: Array,
    filmNews: Array,
    toast: String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    userInfo: app.globalData.userInfo,
    cardCur: 0,
    TabCur1: 0,
    TabCur: 1,
    currentVideo: '',
    channelList: [],
    previewList: [],
    offset: 15,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    buy(event) {
      wx.navigateTo({
        url: '/pages/film/schedule/index?movieinfo='+JSON.stringify(event.currentTarget.dataset.movieinfo),
      })
    },
    search() {
      wx.navigateTo({
        url: '/pages/search/search',
        success: function (res) {},
        fail(res){
          console.log(res);
        }
      })
    },
    movieinfo(event) {
      var myThis = this;
      var id;
      wx.request({
        url: 'https://wx.maoyan.com/hostproxy/mmdb/search/integrated/keyword/list.json?keyword='+event.currentTarget.dataset.movieinfo.name+'&almtype=1&iscorrected=false&stype=-1&ci=20',
        header: {
          'x-host': 'http://maoyanapi.vip.sankuai.com'
        },
        success(res) {
          id = res.data.data[0].list[0].id
          wx.navigateTo({
            url: '/pages/movieinfo/index?maoyanid='+id,
            success: function (res) {
              // 通过eventChannel向被打开页面传送数据
              res.eventChannel.emit('movieinfo', {
                movieinfo: event.currentTarget.dataset.movieinfo
              })
            }
          })
        }
      })
    },
    movetoall(e) {
      wx.reLaunch({
        url: '/pages/index/index?id=1'
      })
    },
    cardSwiper(e) {
      this.setData({
        cardCur: e.detail.current
      })
    },
    tabSelect(e) {
      this.setData({
        TabCur: e.currentTarget.dataset.id,
      })
    },
    tab1Select(e) {
      this.setData({
        TabCur1: e.currentTarget.dataset.id,
      })
    },
    play: function (e) {      
      if (this.data.currentVideo == '') {
        this.setData({
          currentVideo: e.currentTarget.id
        })
      } else {
        if (e.currentTarget.id == this.data.currentVideo) {} else {
          wx.createVideoContext(this.data.currentVideo, this).pause()
          this.setData({
            currentVideo: e.currentTarget.id
          })
        }
      }
    },
    lower() {
      var page = this;
      wx.request({
        url: 'https://m.maoyan.com/asgard/asgardapi/sns/common/feed/channel/list.json?uuid=66F6F470AA1F11EAA9A1E3412A212F2C1EF530EA670449BE9AB82CE331B55E83&channelId=4&feedChannelId=104&timestamp=1591686205322&offset=15',
        success(res) {
          page.setData({
            channelList: [ ...page.data.channelList , ...res.data.data.feeds]
          })          
        }
      })
    },
    lower1() {
      this.setData({offset: this.data.offset + 10})
      var page = this;
      wx.request({
        url: 'https://m.maoyan.com/asgard/asgardapi/sns/common/feed/channel/list.json?uuid=66F6F470AA1F11EAA9A1E3412A212F2C1EF530EA670449BE9AB82CE331B55E83&channelId=4&feedChannelId=1865382&timestamp=1591690572703&offset='+this.data.offset,
        success(res) {
          page.setData({
            previewList: [ ...page.data.previewList , ...res.data.data.feeds]
          })          
        }
      })
    },
  },
  // 组件页面加载
  attached() {
    var page = this;
    wx.request({
      url: 'https://m.maoyan.com/asgard/asgardapi/sns/common/feed/channel/list.json?uuid=66F6F470AA1F11EAA9A1E3412A212F2C1EF530EA670449BE9AB82CE331B55E83&channelId=4&feedChannelId=104&timestamp=1591686205322&offset=15',
      success(res) {
        page.setData({
          channelList: res.data.data.feeds
        })
      }
    })
    wx.request({
      url: 'https://m.maoyan.com/asgard/asgardapi/sns/common/feed/channel/list.json?uuid=66F6F470AA1F11EAA9A1E3412A212F2C1EF530EA670449BE9AB82CE331B55E83&channelId=4&feedChannelId=1865382&timestamp=1591690572703&offset='+this.data.offset,
      success(res) {
        page.setData({
          previewList: res.data.data.feeds
        })        
      }
    })
  }
})